
import sys
import subprocess

class fzf:
    def __init__(self):
        ...

    def _sys_fzf(self, data):
        if not data:
            return None

        data = "\n".join( list(map(str, data)) )

        fzf = subprocess.Popen( ["/usr/bin/fzf"],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        text=True
                        )

        output_data, error = fzf.communicate(input=data)

        if not error:
            return output_data.strip()
        else:
            print(f"FZF: Error {error}", file=sys.stderr)
            sys.exit(1)

    def choice(self, list_: list) -> str:
        return self._sys_fzf(list_)

